package com.base.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.base.po.param.UserData;
import com.base.po.requestParam.SubscribersParam;
import com.base.po.voip.*;
import com.base.po.voip_sdk.SubscribersResponse;
import com.base.service.SubscribersService;
import com.base.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.sqlparam.SubscribersExamplePage;
import org.springframework.stereotype.Service;

/**
 * 用户service实现类
 * 
 * @author Administrator
 *
 */
@Service("subscribersService")
public class SubscribersServiceImpl implements SubscribersService {
	@Autowired
	private SubscribersMapper subscribersMapper;

	@Override
	public int countByExample(SubscribersExample example) {
		return subscribersMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(SubscribersExample example) {
		return subscribersMapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		if (id == null) {
			return 0;
		}
		return subscribersMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(String password, Date logintime, String loginip, Integer role, Boolean datastatus, String mobile,
			String parentid, Date endtime, String pic, Integer type, String shopid, String appstore, Float integral,
			String aliaccount, Boolean alipay, Integer shopBalance, String shopEnddate, String birthday, String gender,
			String email, String address, String username, Boolean isyear, Boolean ismonth, Boolean isquarter) {
		Subscribers record = new Subscribers(Produce.getUUID(), password, Produce.getNowDate(), logintime, loginip,
				role, datastatus, mobile, parentid, endtime, pic, type, shopid, appstore, integral, aliaccount, alipay,
				shopBalance, shopEnddate, birthday, gender, email, address, username, isyear, ismonth, isquarter);

		return subscribersMapper.insert(record);
	}

	@Override
	public int insertSelective(Subscribers record) {
		return subscribersMapper.insertSelective(record);
	}

	@Override
	public List<Subscribers> selectByExample(SubscribersExample example) {
		return subscribersMapper.selectByExample(example);
	}

	@Override
	public List<Subscribers> selectByExamplePage(SubscribersExamplePage example) {
		return subscribersMapper.selectByExamplePage(example);
	}

	@Override
	public List<Subscribers> selectByParentId(String parentid) {
		return subscribersMapper.selectByParentId(parentid);
	}

	@Override
	public List<Subscribers> selectBySharePer(String shareper) {
		return subscribersMapper.selectBySharePer(shareper);
	}

    @Override
    public List<Subscribers> selectBySharePerJSJ(String shareper) {
        return subscribersMapper.selectBySharePerJSJ(shareper);
    }

    @Override
	public Subscribers selectByPrimaryKey(String id) {
		if (id == null || id.isEmpty()) {
			return null;
		}
		return subscribersMapper.selectByPrimaryKey(id);
	}

	@Override
	public Subscribers selectByIsOpen(String isopenmember) {
		return subscribersMapper.selectByIsOpen(isopenmember);
	}

	@Override
	public Subscribers selectByPid(String parentid, String pid) {
		return subscribersMapper.selectByPid(parentid, pid);
	}

	@Override
	public Subscribers selectByRebatePid(String parentid, String rebatepid) {
		return subscribersMapper.selectByRebatePid(parentid, rebatepid);
	}

	@Override
	public Subscribers selectBySpecialId(String parentid, String specialId) {
		return subscribersMapper.selectBySpecialId(parentid, specialId);
	}

	@Override
	public Subscribers selectByPddPid(String parentid, String pddPid) {
		return subscribersMapper.selectByPddPid(parentid, pddPid);
	}

	@Override
	public Subscribers selectByJdPid(String parentid, String jdPid) {
		return subscribersMapper.selectByJdPid(parentid, jdPid);
	}

	@Override
	public List<Subscribers> selectByWxOpenId(String wxOpenId,String ParentId) {
		return subscribersMapper.selectByWxOpenId(wxOpenId,ParentId);
	}

	@Override
	public List<Subscribers> selectByAppleUserId(String appleUserId, String ParentId) {
		return subscribersMapper.selectByAppleUserId(appleUserId, ParentId);
	}

	@Override
	public int updateByExampleSelective(Subscribers record, SubscribersExample example) {
		return subscribersMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(Subscribers record, SubscribersExample example) {
		if (record == null) {
			return 0;
		}
		if (record.getId() == null) {
			return 0;
		}
		return subscribersMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(Subscribers record) {
		if (record == null) {
			return 0;
		}
		if (record.getId() == null) {
			return 0;
		}
		return subscribersMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Subscribers record) {
		if (record == null) {
			return 0;
		}
		if (record.getId() == null) {
			return 0;
		}
		return subscribersMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<String> selectByMobileAndParentId(String mobile, String parentid) {

		return subscribersMapper.selectByMobileAndParentId(mobile, parentid);
	}

	@Override
	public List<String> selectByMobileAndShopId(String mobile, String shopid) {

		return subscribersMapper.selectByMobileAndShopId(mobile, shopid);
	}

	@Override
	public List<Subscribers> selectByAllForExport(String username, String parentid, String shopid, String startTimeFrom, String startTimeTo, Integer start, Integer size) {
		return subscribersMapper.selectByAllForExport(username, parentid, shopid, startTimeFrom, startTimeTo, start, size);
	}


	@Override
	public List<String> selectByMobile(String mobile) {

		return subscribersMapper.selectByMobile(mobile);
	}

	@Override
	public List<Subscribers> selectByShopid(Page page, String shopid, String mobile, String id, Boolean datastatus,
			String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		return subscribersMapper.selectByShopid(page, shopid, mobile, id, datastatus, startTimeFrom, startTimeTo,
				endTimeFrom, endTimeTo);
	}

	@Override
	public int countByShopid(String shopid, String mobile, String id, Boolean datastatus, String startTimeFrom,
			String startTimeTo, String endTimeFrom, String endTimeTo) {
		return subscribersMapper.countByShopid(shopid, mobile, id, datastatus, startTimeFrom, startTimeTo, endTimeFrom,
				endTimeTo);
	}

	@Override
	public Subscribers selectBySubscribers(String mobile, String parentid) {
		return subscribersMapper.selectBySubscribers(mobile, parentid);
	}

	@Override
	public Subscribers countByShopSA(String shopid, String mobile, String username, String shopname) {
		return subscribersMapper.countByShopSA(shopid, mobile, username, shopname);
	}

	@Override
	public List<Subscribers> selectByShopSA(Page page, String shopid, String mobile, String username, String shopname) {
		return subscribersMapper.selectByShopSA(page, shopid, mobile, username, shopname);
	}

	@Override
	public Subscribers countByShopPA(String parentid, String mobile, String username, String shopname) {
		return subscribersMapper.countByShopPA(parentid, mobile, username, shopname);
	}

	@Override
	public List<Subscribers> selectByShopPA(Page page, String parentid, String mobile, String username,
			String shopname) {
		return subscribersMapper.selectByShopPA(page, parentid, mobile, username, shopname);
	}

	@Override
	public Subscribers countByCallSA(String shopid, String mobile, String username, String shopname) {
		return subscribersMapper.countByCallSA(shopid, mobile, username, shopname);
	}

	@Override
	public List<Subscribers> selectByCallSA(Page page, String shopid, String mobile, String username, String shopname) {
		return subscribersMapper.selectByCallSA(page, shopid, mobile, username, shopname);
	}

	@Override
	public Subscribers countByCallPA(String parentid, String mobile, String username, String shopname) {
		return subscribersMapper.countByCallPA(parentid, mobile, username, shopname);
	}

	@Override
	public List<Subscribers> selectByCallPA(Page page, String parentid, String mobile, String username,
			String shopname) {
		return subscribersMapper.selectByCallPA(page, parentid, mobile, username, shopname);
	}

	@Override
	public int countByPer(String perid) {
		return subscribersMapper.countByPer(perid);
	}

	@Override
	public List<Subscribers> selectByPer(Page page, String perid) {
		return subscribersMapper.selectByPer(page, perid);
	}

	@Override
	public int selectByBonus(String parentid, String shopid, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.selectByBonus(parentid, shopid, bonus1level, bonus2level, bonus3level);
	}

	@Override
	public List<Subscribers> selectBonusByAll(Page page, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.selectBonusByAll(page, bonus1level, bonus2level, bonus3level);
	}

	@Override
	public List<Subscribers> selectBonusByParentid(Page page, String parentid, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.selectBonusByParentid(page, parentid, bonus1level, bonus2level, bonus3level);
	}

	@Override
	public List<Subscribers> selectBonusByShopidNotAll(Page page, String shopid, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.selectBonusByShopidNotAll(page, shopid, bonus1level, bonus2level, bonus3level);
	}

	@Override
	public int countBonusByAll(Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.countBonusByAll(bonus1level, bonus2level, bonus3level);
	}

	@Override
	public int countBonusByParentid(String parentid, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.countBonusByParentid(parentid, bonus1level, bonus2level, bonus3level);
	}

	@Override
	public int countBonusByShopidNotAll(String shopid, Integer bonus1level, Integer bonus2level, Integer bonus3level) {
		return subscribersMapper.countBonusByShopidNotAll(shopid, bonus1level, bonus2level, bonus3level);
	}

	/**
	 *
	 * @param parentId   代理商ID
	 * @return
	 */
	@Override
	public List<Agentinfoes> findParentAgentInfo(String parentId) {
		return subscribersMapper.findParentAgentInfo(parentId);
	}

	@Override
	public List<Subscribers> selectNextPer1(String userId) {
		return subscribersMapper.selectNextPer1(userId);
	}

	@Override
	public List<Subscribers> selectNextPer2(String userId) {
		return subscribersMapper.selectNextPer2(userId);
	}

	@Override
	public List<Subscribers> selectNextPer3(String userId) {
		return subscribersMapper.selectNextPer3(userId);
	}

	@Override
	public List<Subscribers> selectNextPer4(String userId) {
		return subscribersMapper.selectNextPer4(userId);
	}

	@Override
	public List<Subscribers> selectNextPer5(String userId) {
		return subscribersMapper.selectNextPer5(userId);
	}

	@Override
	public Subscribers selectsomeByPrimaryKey(String userid) {
		return subscribersMapper.selectsomeByPrimaryKey(userid);
	}

	@Override
	public List<Subscribers> selectByShopIdOnly(String shopid) {
		return subscribersMapper.selectByShopidOnly(shopid);
	}

	@Override
	public int updateWxUntying(String id) {
		return subscribersMapper.updateWxUntying(id);
	}

	@Override
	public int hasSub(String mobile, String parentId) {
		return subscribersMapper.hasSub(mobile,parentId);
	}

	@Override
	public List<String> selectMobiles(String userid) {
		return subscribersMapper.selectMobiles(userid);
	}

	@Override
	public int updateIsUserAuthById(String id) {
		return subscribersMapper.updateIsUserAuthById(id);
	}

	@Override
	public List<String> selectForShareId(String shareId) {
		return subscribersMapper.selectForShareId(shareId);
	}

	@Override
	public List<String> selectMemberForShareId(String shareId) {
		return subscribersMapper.selectMemberForShareId(shareId);
	}

	@Override
	public List<String> selectForSharePer(List<String> list) {
		return subscribersMapper.selectForSharePer(list);
	}

	@Override
	public List<String> selectForTeamCommission(String shareId, List<String> list) {
		return subscribersMapper.selectForTeamCommission(shareId, list);
	}

	@Override
	public List<Subscribers> selectForDirect(String shareId, Page page) {
		return subscribersMapper.selectForDirect(shareId, page);
	}

	@Override
	public int countForDirect(String shareId) {
		return subscribersMapper.countForDirect(shareId);
	}

	@Override
	public Boolean isUserAuth(String id) {
		Subscribers subscribers = subscribersMapper.selectByPrimaryKey(id);
		if (subscribers.getIsUserAuth() == 1){
			return true;
		}else {
			return false;
		}
	}

	@Override
	public List<String> findParList(String userId) {
		// 获取所有用户
		List<String> teamUserId = new ArrayList<>();

		String parUsersId = subscribersMapper.findParList(userId);
		String[] parUsersIds = parUsersId.split(",");
		for (int i = 1;i < parUsersIds.length; i++){
			teamUserId.add(parUsersIds[i]);
		}

		return teamUserId;
	}

	@Override
	public List<String> findChildList(String userId) {
		// 直推用户信息
		List<String> subUserId = subscribersMapper.selectForShareId(userId);

		// 查询的用户
		List<String> findUserId = new ArrayList<>();
		// 获取所有用户
		List<String> teamUserId = new ArrayList<>();
		teamUserId.addAll(subUserId);

		boolean flag = true;
		int lastSize = 0;//下面逻辑查找该用户全部下级长度
		int nowSize = 0;
		while (flag) {
			if (subUserId.size() > 0) {
				findUserId = subscribersMapper.selectForSharePer(subUserId);

				teamUserId.addAll(findUserId);

				subUserId = findUserId;
				nowSize = teamUserId.size();
				if (nowSize == lastSize) {
					flag = false;
				} else {
					lastSize = nowSize;
				}
			} else {
				flag = false;
			}
		}

		return teamUserId;
	}

	@Override
	public int selectByCount(String parentid, Date begin, Date end) {
		return subscribersMapper.selectByCount(parentid, begin, end);
	}

	@Override
	public int selectByCountShop(String shopid, Date begin, Date end) {
		return subscribersMapper.selectByCountShop(shopid, begin, end);
	}

	@Override
	public List<Subscribers> selectBySub(String mobile, String parentid) {
		
		return subscribersMapper.selectBySub(mobile, parentid);
	}

	@Override
	public List<SubscribersResponse> selectBySubForSbd(String mobile, String parentid) {
		return subscribersMapper.selectBySubForSbd(mobile, parentid);
	}

	@Override
	public List<Subscribers> selectAllSub(String parentid, Integer isnull) {
		return subscribersMapper.selectAllSub(parentid, isnull);
	}

	@Override
	public List<Subscribers> selectByAll(Page page, String shareper, String mobile, String username, String parentid, String shopid,
			Boolean datastatus, String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		
		return subscribersMapper.selectByAll(page, shareper, mobile, username, parentid, shopid, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}

	@Override
	public List<Subscribers> selectByParentid(Page page, String parentid, String shareper, String mobile, String username, String id,
			String shopid, Boolean datastatus, String startTimeFrom, String startTimeTo, String endTimeFrom,
			String endTimeTo) {
		
		return subscribersMapper.selectByParentid(page, parentid, shareper, mobile, username, id, shopid, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}

	@Override
	public List<Subscribers> selectByShopidNotAll(Page page, String shopid, String shareper, String mobile, String username, String id,
			Boolean datastatus, String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		
		return subscribersMapper.selectByShopidNotAll(page, shopid, shareper, mobile, username, id, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}

	@Override
	public int countByAll(String shareper, String mobile, String username, String parentid, String shopid, Boolean datastatus,
			String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		
		return subscribersMapper.countByAll(shareper, mobile, username, parentid, shopid, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}

	@Override
	public int countByParentid(String parentid, String shareper, String mobile, String username, String id, String shopid,
			Boolean datastatus, String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		
		return subscribersMapper.countByParentid(parentid, shareper, mobile, username, id, shopid, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}

	@Override
	public int countByShopidNotAll(String shopid, String shareper, String mobile, String username, String id, Boolean datastatus,
			String startTimeFrom, String startTimeTo, String endTimeFrom, String endTimeTo) {
		
		return subscribersMapper.countByShopidNotAll(shopid, shareper, mobile, username, id, datastatus, startTimeFrom, startTimeTo, endTimeFrom, endTimeTo);
	}


	@Override
	public int update(SubscribersParam param) {
		if (StringUtils.isNotEmpty(param.getPassword())){
			param.setPassword(PasswordUtils.toPWD(param.getPassword()));
		}
		Subscribers subscribers = new Subscribers();
		subscribers.setId(param.getId());
		subscribers.setRealname(param.getRealname());
		subscribers.setBankaccount(param.getBanknum());
		subscribers.setPassword(param.getPassword());
		subscribers.setPic(param.getImg());
		subscribers.setUsername(param.getUsername());
		return subscribersMapper.updateByPrimaryKeySelective(subscribers);
	}

	@Override
	public List<ChildrenAgent> getChildrenAgent(String userId) {
		return subscribersMapper.getChildrenAgent(userId);
	}


	@Override
	public Integer getChildrenAgentNum(List<ChildrenAgent> list) {

		Integer num = 0;
		for (ChildrenAgent agent : list) {
			num = getChildrentNum(agent,num) + 1;
			System.out.println(num);
		}
		return num;
	}

	@Override
	public Integer countByMenber() {
		return subscribersMapper.countByMenber();
	}

	private Integer getChildrentNum(ChildrenAgent agent , Integer num) {
		if (0 == agent.getChildren().size()){
			return num;
		}
		for (ChildrenAgent child : agent.getChildren()) {
			num = getChildrentNum(child,num);
		}
		return num + agent.getChildren().size();
	}

	@Override
	public List<Daili> selectDaili(Page page) {
		return subscribersMapper.selectDaili(page);
	}

	@Override
	public BigDecimal getTeamTotalPrice(String userId) {
		return subscribersMapper.getTeamTotalPrice(userId);
	}

	@Override
	public Integer getTeamPtSuccessNum(String userId) {
		return subscribersMapper.getTeamPtSuccessNum(userId);
	}

	@Override
	public UserData selectForLevelUp(String userId) {
		return subscribersMapper.selectForLevelUp(userId);
	}
}
